package com.weipeep.sys.controller;


import java.util.Arrays;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.weipeep.sys.domain.NewsCategoryDO;
import com.weipeep.sys.service.NewsCategoryService;
import com.weipeep.common.utils.Result;

/**
 * 
 * <pre>
 * 
 * </pre>
 * <small> 2018-09-18 15:45:39 | wangyun</small>
 */
@Controller
@RequestMapping("/sys/newsCategory")
public class NewsCategoryController {
	@Autowired
	private NewsCategoryService newsCategoryService;
	
	@GetMapping()
	@RequiresPermissions("sys:newsCategory:newsCategory")
	String NewsCategory(){
	    return "sys/newsCategory/newsCategory";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("sys:newsCategory:newsCategory")
	public Result<Page<NewsCategoryDO>> list(Integer pageNumber, Integer pageSize, NewsCategoryDO newsCategoryDTO){
		// 查询列表数据
        Page<NewsCategoryDO> page = new Page<>(pageNumber, pageSize);
        
        Wrapper<NewsCategoryDO> wrapper = new QueryWrapper<NewsCategoryDO>(newsCategoryDTO);
        page = (Page<NewsCategoryDO>) newsCategoryService.page(page, wrapper);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("sys:newsCategory:add")
	String add(){
	    return "sys/newsCategory/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("sys:newsCategory:edit")
	String edit(@PathVariable("id") Long id,Model model){
		NewsCategoryDO newsCategory = newsCategoryService.getById(id);
		model.addAttribute("newsCategory", newsCategory);
	    return "sys/newsCategory/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("sys:newsCategory:add")
	public Result<String> save( NewsCategoryDO newsCategory){
		if(newsCategoryService.save(newsCategory)){
			return Result.ok();
		}
		return Result.fail();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("sys:newsCategory:edit")
	public Result<String>  update( NewsCategoryDO newsCategory){
		newsCategoryService.updateById(newsCategory);
		return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("sys:newsCategory:remove")
	public Result<String>  remove( Long id){
		if(newsCategoryService.removeById(id)){
            return Result.ok();
		}
		return Result.fail();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("sys:newsCategory:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		newsCategoryService.removeByIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
